package com.ruoyi.web.controller.net;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.net.domain.DeviceStatusIsisNeighb;
import com.ruoyi.net.domain.NetElement;
import com.ruoyi.net.netconf.util.DeviceManager;
import com.ruoyi.net.service.INetElementService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.net.domain.DeviceStatusTunnelProtect;
import com.ruoyi.net.service.IDeviceStatusTunnelProtectService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * 设备状态-隧道内保护详细状态Controller
 * 
 * @author tony
 * @date 2024-12-18
 */
@Api(tags = "设备状态-隧道内保护详细状态")
@RestController
@RequestMapping("/device/status/tunnel/protect")
public class DeviceStatusTunnelProtectController extends BaseController
{
    @Autowired
    private IDeviceStatusTunnelProtectService deviceStatusTunnelProtectService;

    @Autowired
    private INetElementService netElementService;

    @Autowired
    private ISysConfigService sysConfigService;

    @Autowired
    private DeviceManager deviceManager;

    /**
     * 查询设备状态-隧道内保护详细状态列表
     */
    @ApiOperation(value = "读取设备状态-隧道内保护详细状态列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:protect:list')")
    @GetMapping("/read")
    public TableDataInfo read(DeviceStatusTunnelProtect deviceStatusTunnelProtect) {
        List<DeviceStatusTunnelProtect> deviceStatusTunnelProtects = new ArrayList<>();
        StringBuilder xml = new StringBuilder();
        xml.append("<filter type=\"subtree\">" +
                "           <mpls-oper xmlns=\"http://fiberhome.com/fhnw/yang/mplsprotocol/fos-mpls-oper\">" +
                "    <tunnel-protect-groups>" +
                "        <tunnel-protect-detail>");
        if (StringUtils.isNotEmpty(deviceStatusTunnelProtect.getTunnelId())) {
            xml.append("     <tunnel-id>")
                    .append(deviceStatusTunnelProtect.getTunnelId())
                    .append("</tunnel-id>");
        }
//                "            <tunnel-id>1</tunnel-id>" +
                /*"            <ingress></ingress> 首节点路由ID" +
                "            <egress></egress> 尾节点路由ID" +
                "            <primary-lsp-id></primary-lsp-id> 主lsp id" +
                "            <secondary-lsp-id></secondary-lsp-id> 备lsp id" +
                "            <switch-state></switch-state> 切换状态" +
                "            <primary-lsp-state></primary-lsp-state> 主lsp状态" +
                "            <secondary-lsp-state></secondary-lsp-state> 备lsp状态" +
                "            <return-mode></return-mode> 返回模式" +
                "            <signal-protocol></signal-protocol> 隧道所属协议" +
                "            <hold-off-config-time></hold-off-config-time> 拖延时间" +
                "            <wtr-config-time></wtr-config-time> 等待恢复时间" +
                "            <primary-lsp-label></primary-lsp-label> 主lsp出标签" +
                "            <secondary-lsp-label></secondary-lsp-label> 备lsp出标签" +
                "            <primary-if-name></primary-if-name> 主lsp出接口" +
                "            <secondary-if-name></secondary-if-name> 备lsp出接口" +
                "            <protect-type></protect-type> 保护类型" +
                "            <rx-aps></rx-aps> 收aps" +
                "            <tx-aps></tx-aps> 发aps" +
                "            <alm-detect-mode></alm-detect-mode> 告警检测方式" +
                "            <pri-detect-id></pri-detect-id> 主用ID" +
                "            <secondary-detect-id></secondary-detect-id> 备用ID" +
                "            <protect-entity-id></protect-entity-id> 保护实体索引" +*/
        xml.append("        </tunnel-protect-detail>" +
                "    </tunnel-protect-groups>" +
                "</mpls-oper>" +
                "     </filter>");
        NetElement netElement = netElementService.getCacheObject(deviceStatusTunnelProtect.getElementId());
        if (sysConfigService.selectDebugEnabled()) {
            deviceManager.getDevice(netElement.getIp());
            String rpcReply = deviceManager.getStatus(xml.toString());
            // 解析xml
            NodeList list = findNetConfXmlNodes(rpcReply, "//tunnel-protect-groups/tunnel-protect-detail/tunnel-id");
            if (list != null && list.getLength() > 0) {
                for (int i = 0; i < list.getLength(); i++) {
                    DeviceStatusTunnelProtect entity = new DeviceStatusTunnelProtect();
                    Node node = list.item(i);
                    // 通过标签名获取节点的值，中间系统标识
                    entity.setTunnelId(getXmlLabelValue(node, "tunnel-id"));
//                           <ingress></ingress> 首节点路由ID" +
                    entity.setIngress(getXmlLabelValue(node, "ingress"));
//                "            <egress></egress> 尾节点路由ID" +
                    entity.setEgress(getXmlLabelValue(node, "egress"));
//                "            <primary-lsp-id></primary-lsp-id> 主lsp id" +
                    entity.setPrimaryLspId(getXmlLabelValue(node, "primary-lsp-id"));
//                "            <secondary-lsp-id></secondary-lsp-id> 备lsp id" +
                    entity.setSecondaryLspId(getXmlLabelValue(node, "secondary-lsp-id"));
//                "            <switch-state></switch-state> 切换状态" +
                    entity.setSwitchState(getXmlLabelValue(node, "switch-state"));
//                "            <primary-lsp-state></primary-lsp-state> 主lsp状态" +
                    entity.setPrimaryLspState(getXmlLabelValue(node, "primary-lsp-state"));
//                "            <secondary-lsp-state></secondary-lsp-state> 备lsp状态" +
                    entity.setSecondaryLspState(getXmlLabelValue(node, "econdary-lsp-state"));
//                "            <return-mode></return-mode> 返回模式" +
                    entity.setReturnMode(getXmlLabelValue(node, "return-mode"));
//                "            <signal-protocol></signal-protocol> 隧道所属协议" +
                    entity.setSignalProtocol(getXmlLabelValue(node, "signal-protocol"));
//                "            <hold-off-config-time></hold-off-config-time> 拖延时间" +
                    entity.setHoldOffConfigTime(getXmlLabelValue(node, "hold-off-config-time"));
//                "            <wtr-config-time></wtr-config-time> 等待恢复时间" +
                    entity.setWtrConfigTime(getXmlLabelValue(node, "wtr-config-time"));
//                "            <primary-lsp-label></primary-lsp-label> 主lsp出标签" +
                    entity.setPrimaryLspLabel(getXmlLabelValue(node, "primary-lsp-label"));
//                "            <secondary-lsp-label></secondary-lsp-label> 备lsp出标签" +
                    entity.setSecondaryLspLabel(getXmlLabelValue(node, "secondary-lsp-label"));
//                "            <primary-if-name></primary-if-name> 主lsp出接口" +
                    entity.setPrimaryIfName(getXmlLabelValue(node, "primary-if-name"));
//                "            <secondary-if-name></secondary-if-name> 备lsp出接口" +
                    entity.setSecondaryIfName(getXmlLabelValue(node, "secondary-if-name"));
//                "            <protect-type></protect-type> 保护类型" +
                    entity.setProtectType(getXmlLabelValue(node, "protect-type"));
//                "            <rx-aps></rx-aps> 收aps" +
                    entity.setRxAps(getXmlLabelValue(node, "rx-aps"));
//                "            <tx-aps></tx-aps> 发aps" +
                    entity.setTxAps(getXmlLabelValue(node, "tx-aps"));
//                "            <alm-detect-mode></alm-detect-mode> 告警检测方式" +
                    entity.setAlmDetectMode(getXmlLabelValue(node, "alm-detect-mode"));
//                "            <pri-detect-id></> 主用ID" +
                    entity.setPriDetectId(getXmlLabelValue(node, "pri-detect-id"));
//                "            <secondary-detect-id></secondary-detect-id> 备用ID" +
                    entity.setSecondaryDetectId(getXmlLabelValue(node, "secondary-detect-id"));
//                "            <protect-entity-id></protect-entity-id> 保护实体索引"
                    entity.setProtectEntityId(getXmlLabelValue(node, "protect-entity-id"));
                    deviceStatusTunnelProtects.add(entity);
                }
            }
        }
        startPage();
        return getDIYPageTable(deviceStatusTunnelProtects);
    }

    /**
     * 查询设备状态-隧道内保护详细状态列表
     */
    @ApiOperation(value = "查询设备状态-隧道内保护详细状态列表", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:protect:list')")
    @GetMapping("/list")
    public TableDataInfo list(DeviceStatusTunnelProtect deviceStatusTunnelProtect)
    {
        startPage();
        List<DeviceStatusTunnelProtect> list = deviceStatusTunnelProtectService.selectDeviceStatusTunnelProtectList(deviceStatusTunnelProtect);
        return getDataTable(list);
    }

    /**
     * 导出设备状态-隧道内保护详细状态列表
     */
    @ApiOperation(value = "导出设备状态-隧道内保护详细状态列表", httpMethod = "POST")
//    @PreAuthorize("@ss.hasPermi('net:protect:export')")
    @Log(title = "设备状态-隧道内保护详细状态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DeviceStatusTunnelProtect deviceStatusTunnelProtect)
    {
        List<DeviceStatusTunnelProtect> list = deviceStatusTunnelProtectService.selectDeviceStatusTunnelProtectList(deviceStatusTunnelProtect);
        ExcelUtil<DeviceStatusTunnelProtect> util = new ExcelUtil<DeviceStatusTunnelProtect>(DeviceStatusTunnelProtect.class);
        util.exportExcel(response, list, "设备状态-隧道内保护详细状态数据");
    }

    /**
     * 获取设备状态-隧道内保护详细状态详细信息
     */
    @ApiOperation(value = "获取设备状态-隧道内保护详细状态详细信息", httpMethod = "GET")
//    @PreAuthorize("@ss.hasPermi('net:protect:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(deviceStatusTunnelProtectService.selectDeviceStatusTunnelProtectById(id));
    }

    /**
     * 新增设备状态-隧道内保护详细状态
     */
    @ApiOperation(value = "新增设备状态-隧道内保护详细状态", httpMethod = "POST")
//    @PreAuthorize("@ss.hasPermi('net:protect:add')")
    @Log(title = "设备状态-隧道内保护详细状态", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DeviceStatusTunnelProtect deviceStatusTunnelProtect)
    {
        return toAjax(deviceStatusTunnelProtectService.insertDeviceStatusTunnelProtect(deviceStatusTunnelProtect));
    }

    /**
     * 修改设备状态-隧道内保护详细状态
     */
    @ApiOperation(value = "修改设备状态-隧道内保护详细状态", httpMethod = "PUT")
//    @PreAuthorize("@ss.hasPermi('net:protect:edit')")
    @Log(title = "设备状态-隧道内保护详细状态", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DeviceStatusTunnelProtect deviceStatusTunnelProtect)
    {
        return toAjax(deviceStatusTunnelProtectService.updateDeviceStatusTunnelProtect(deviceStatusTunnelProtect));
    }

    /**
     * 删除设备状态-隧道内保护详细状态
     */
    @ApiOperation(value = "删除设备状态-隧道内保护详细状态", httpMethod = "DELETE")
//    @PreAuthorize("@ss.hasPermi('net:protect:remove')")
    @Log(title = "设备状态-隧道内保护详细状态", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(deviceStatusTunnelProtectService.deleteDeviceStatusTunnelProtectByIds(ids));
    }
}
